package org.example.logindemo.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.example.logindemo.bean.User;
import org.example.logindemo.bean.UserDTO;
import org.example.logindemo.exception.PasswordErrorException;
import org.example.logindemo.mapper.UserMapper;
import org.example.logindemo.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.logindemo.exception.AccountNotFoundException;
import org.example.logindemo.constant.MessageConstant;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserMapper userMapper;

    public User login(UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        //1、根据用户名查询数据库中的数据

        User user = userMapper.getByUsername(username);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            // 账号不存在
            User errorUser = new User();
            errorUser.setMessage("账号不存在");
            errorUser.setSuccess(false);
            return errorUser;
        }

        // 检查密码
        if (!password.equals(user.getPassword())) {
            // 密码错误
            user.setMessage("密码错误");
            user.setSuccess(false);
            return user;
        }

        // 登录成功
        user.setMessage("登录成功");
        user.setSuccess(true);
        return user;
    }
}
